package my.edu.model.bills.purchase.controller;

import cn.hutool.json.JSONObject;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.groups.Default;
import my.edu.common.domain.PageVO;
import my.edu.common.validate.groups.Insert;
import my.edu.common.domain.PageDTO;
import my.edu.common.domain.Response;
import my.edu.common.validate.groups.Purchase;
import my.edu.model.bills.purchase.entity.dto.PurchaseReturnDTO;
import my.edu.model.bills.purchase.entity.dto.PurchaseReturnOrderSearchDTO;
import my.edu.model.bills.purchase.entity.vo.PurchaseReturnOrderVO;
import my.edu.model.bills.purchase.service.IPurchaseReturnOrderService;
import my.edu.utils.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/purchaseReturnOrder")
@Tag(name = "采购退货订单")
@Validated
public class PurchaseReturnOrderController {
    @Autowired
    private IPurchaseReturnOrderService purchaseReturnOrderService;

    @PutMapping
    @RequiresPermissions("purchaseReturn:save")
    @Operation(summary = "新增订单")
    public Response addPurchaseReturnOrder(@RequestBody @Validated(value = {Insert.class}) PurchaseReturnDTO purchaseReturnDTO) {
        purchaseReturnOrderService.saveNew(purchaseReturnDTO);
        return Response.success();
    }


    @DeleteMapping("/deleteBatch")
    @RequiresPermissions("purchaseReturn:del")
    @Operation(summary = "批量删除订单")
    public Response delPurchaseOrder(String ids) {
        List<Long> longs = StringUtils.strToLongList(ids);
        purchaseReturnOrderService.removeOrder(longs);
        return Response.success();
    }

    @DeleteMapping("/delete")
    @RequiresPermissions("purchaseReturn:del")
    @Operation(summary = "删除订单")
    public Response delete(Long id) {
        purchaseReturnOrderService.removeOrder(List.of(id));
        return Response.success();
    }


    @PostMapping("/update")
    @RequiresPermissions("purchaseReturn:update")
    @Operation(summary = "修改订单")
    public Response updatePurchaseReturnOrder(@RequestBody @Validated PurchaseReturnDTO purchaseReturnDTO) {
        purchaseReturnOrderService.updateOrder(purchaseReturnDTO);
        return Response.success();
    }

    @PostMapping("/batchSetStatus")
    @RequiresPermissions(value = {"purchaseReturn:audit"})
    @Operation(summary = "更新审核状态")
    public Response batchSetStatus(@RequestBody JSONObject jsonObject){
        int status = jsonObject.getInt("status");
        String ids = jsonObject.getStr("ids");
        List<Long> longs = StringUtils.strToLongList(ids);
        if(status==1){
            purchaseReturnOrderService.auditOrder(longs);
        } else if (status==0) {
            purchaseReturnOrderService.rauditOrder(longs);
        } else{
            return Response.error("状态值错误");
        }
        return Response.success();
    }


    @GetMapping("/list")
    @RequiresPermissions("purchaseReturn:get")
    @Operation(summary = "查询订单")
    public Response getPurchaseReturnrder(@ModelAttribute @Validated PageDTO page,
                                     @ModelAttribute PurchaseReturnOrderSearchDTO purchaseReturnOrderSearchDTO) {
        PageVO<PurchaseReturnOrderVO> purchaseOrder = purchaseReturnOrderService.getPurchaseOrder(page, purchaseReturnOrderSearchDTO);
        return Response.success(purchaseOrder);
    }



    @GetMapping("/one")
    @RequiresPermissions("purchaseReturn:get")
    @Operation(summary = "根据id查询查询订单")
    public Response getPurchaseReturnOrder( @NotNull(message = "id不能为空") Long id) {
        return Response.success(purchaseReturnOrderService.getOrderOne(id));
    }

    @GetMapping
    @Operation(summary = "查询全部订单")
    public Response getPurchaseReturnrderOnly(@ModelAttribute PurchaseReturnOrderSearchDTO purchaseReturnOrderSearchDTO) {
        return Response.success( purchaseReturnOrderService.listOnly(purchaseReturnOrderSearchDTO));
    }
}
